<?php

/*
 * 需要用到用户信息的控制器则使用MY_Controlle，否则直接使用CI_Controller
 */
class MY_Controller extends CI_Controller
{

    protected $per_page = 20;
 // 分页数据
    protected $uid = '';

    protected $data = array();
 // 用于json格式返回
    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('session');
        $this->_init();
    }

    /**
     * 需要用户登录的类调用
     */
    protected function _init()
    {
        $this->uid = $this->session->userdata('uid');
        // 未登录则记录uri
        if (! $this->uid) {
            // 需要排除ajax链接
            $ignore = array(
                '/pay/pingpp_ini_wechat'
            );
            
            if (in_array($_SERVER['REQUEST_URI'], $ignore)) {
                // 直接进入action
            } else {
                $this->_session_arr['returnUri'] = $_SERVER['REQUEST_URI'];
                $this->session->set_userdata($this->_session_arr);
                
                // 进行登录
                $this->_rediret('user/login');
            }
        }
        
        // 统计页面访问比例
        /*
         * if(0){
         * $this->load->library('redislib');
         *
         * $key = $_SERVER['HTTP_HOST'].'|'.$this->router->class.'|'.$this->router->method;
         * $history = $this->redislib->get($key);
         * if($history){
         * $this->redislib->set($key,$history+1);
         * }else{
         * $this->redislib->set($key,1);
         * }
         * }
         */
    }

    protected function _rediret($uri)
    {
        if (substr($uri, 0, 5) == 'http:' || substr($uri, 0, 6) == 'https:') {
            $url = $uri;
        } else {
            $url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $uri;
        }
        // dump($url);
        header("Location: {$url}");
    }
    
    // 统一json返回
    protected function _json_back($msg = '', $status = 0)
    {
        $json = array(
            'status' => $status,
            'msg' => $msg,
            'data' => $this->data
        );
        // 手动结束
        if ($this->db) {
            $this->db->close();
        }
        
        header("Content-Type:application/json;charset=utf-8");
        die(json_encode($json));
    }

    protected function _getcontent($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $contents = curl_exec($ch);
        if (curl_errno($ch)) {
            $contents = '';
        } else {
            curl_close($ch);
        }
        
        return $contents;
    }

    protected function set_opennid()
    {}
    
    // 调试日志
    function log_re($content = '')
    {
        // 按照域名区分
        $log = $_SERVER['DOCUMENT_ROOT'] . '/mylog/log.txt';
        
        file_put_contents($log, date('Y-m-d H:i:s') . '|' . ':' . $content . "\n", FILE_APPEND);
    }
    
    // $backuri回调地址的uri,$state,回调时候自带参数.
    protected function _wechat_root($backuri, $state = '')
    {
        $this->config->load('wechat', TRUE);
        $wechat = $this->config->item('wechat', 'wechat');
        $appid = $wechat['appid'];
        
        $backurl = urlencode('http://' . $_SERVER['HTTP_HOST'] . '/' . $backuri);
        
        // 使用*进行分割参数
        $this->_rediret("https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$backurl}&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect");
    }

    protected function _get_openid()
    {
        if (isset($_SESSION['openid'])) {
            return $_SESSION['openid'];
        } elseif ($this->uid) {
            $openid = $this->common_model->getone_byid($this->uid, 'users', 'open_id');
            if ($openid) {
                return $openid['open_id'];
            }
        }
        
        return false;
    }
}
